package com.xinghou.mq.impl;

import com.xinghou.mq.CreateUpdateInterface;
import com.xinghou.mq.UserGeoInterface;
import com.xinghou.platform.dao.main.*;
import com.xinghou.platform.lucene.main.UserLuceneService;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.model.main.UserGeo;
import com.xinghou.platform.model.main.UserLocation_Geo;
import com.xinghou.platform.model.main.UserStatistics;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Closeable;
import java.io.IOException;

@Service("mqUserGeoService")
public class UserGeoInterfaceImpl implements UserGeoInterface, Closeable {
	@Resource
	private UserDAO userDAO;

	@Resource
	private CityDAO cityDAO;
	@Resource
	private StoreDAO storeDAO;

	@Resource
	private UserLuceneService userLuceneService;



	@Resource
	private UserStatisticsDAO userStatisticsDAO;

	@Resource
	private UserGeoDAO userGeoDAO;

	public UserGeoInterfaceImpl() {

	}

	@Override
	public void update(int userId, boolean update) {

		if (update) {

			UserGeo geo = userGeoDAO.getUserGeoById(userId);
			User user = userDAO.getUserById(userId);
			UserStatistics sts = userStatisticsDAO.getUserStatById(userId);

			UserLocation_Geo userLocation_Geo = new UserLocation_Geo();

			userLocation_Geo.setUserId(userId);
			userLocation_Geo.setUserType(user.getUsertype().intValue());
			userLocation_Geo.setCityCode(user.getCitycode());
			userLocation_Geo.setPos(new Double[] { geo.getLongitude(), geo.getLatitude() });
			userLocation_Geo.setStoreid(user.getStoreid());
			userLocation_Geo.setLastLoginTime(sts.getLastlogintime());


		} else {
			UserGeo geo = userGeoDAO.getUserGeoById(userId);
			User user = userDAO.getUserById(userId);
			UserStatistics sts = userStatisticsDAO.getUserStatById(userId);
			UserLocation_Geo userLocation_Geo = new UserLocation_Geo();

			userLocation_Geo.setUserId(userId);
			userLocation_Geo.setUserType(user.getUsertype().intValue());
			userLocation_Geo.setCityCode(user.getCitycode());
			userLocation_Geo.setPos(new Double[] { geo.getLongitude(), geo.getLatitude() });
			userLocation_Geo.setStoreid(user.getStoreid());
			userLocation_Geo.setLastLoginTime(sts.getLastlogintime());

			//userLocationMONGO.insert(userLocation_Geo);
		}
	}

	@Override
	public void close() throws IOException {

	}

}
